package com.lot.ws.simulator;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import com.lot.ws.dto.ReadingDetails;

public class DataSimulator {

	static ScheduledExecutorService serv1 = Executors
			.newSingleThreadScheduledExecutor();
	static ScheduledExecutorService serv2 = Executors
			.newSingleThreadScheduledExecutor();
	static ScheduledExecutorService serv3 = Executors
			.newSingleThreadScheduledExecutor();
	static ScheduledExecutorService serv4 = Executors
			.newSingleThreadScheduledExecutor();


	public static final ReadingDetails srcs[] = {
			new ReadingDetails("MS-R1", "home", "12300", "Miami"),
			new ReadingDetails("Ucl", "home", "12311", "London"),
			new ReadingDetails("MS-R2", "hospital", "12322", "Leicester"),
			new ReadingDetails("MS-R3", "hospital", "12333", "London"),
			new ReadingDetails("Ucl", "home", "12344", "London")
			};

	static Date start;
	static Date end;
	
	public static void main(String a[]) {
		try {
			start = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH).parse("08/09/2013 00:00:00");
			end = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH).parse("08/09/2013 23:55:00");
//			end = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH).parse("08/09/2013 00:16:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
//		try {
//			System.out.println(srcs[0] + " scheduled.");
//			serv1.scheduleAtFixedRate(new DataGenerator(srcs[0]), 0, 1, TimeUnit.MINUTES);10 sept, 11:00 10 sept, 11:01
//			serv2.scheduleAtFixedRate(new DataGenerator(srcs[1]), 0, 1, TimeUnit.MINUTES);10 sept, 11:00 10 sept, 11:01
//			serv3.scheduleAtFixedRate(new DataGenerator(srcs[2]), 0, 1, TimeUnit.MINUTES);10 sept, 11:00 10 sept, 11:01
//			serv4.scheduleAtFixedRate(new DataGenerator(srcs[3]), 0, 1, TimeUnit.MINUTES);10 sept, 11:00
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			System.out.println("Terminating............");
////				serv1.shutdown();
////				serv3.shutdown();
//		}
//		
		Date curr = start;
		while (true) {
			CountDownLatch latch = new CountDownLatch(5);
			
			new DataGenerator(srcs[0], curr, latch).run();
			new DataGenerator(srcs[1], curr, latch).run();
			new DataGenerator(srcs[2], curr, latch).run();
			new DataGenerator(srcs[3], curr, latch).run();
			new DataGenerator(srcs[4], curr, latch).run();
			try {
				latch.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			curr = nextMin(curr);
			if(curr.after(end)){
				break;
			}
			//change date
		}
		System.out.println("--------------------------   FINISHED  --------------------------");
	}

	
	public static Date nextMin(Date to)
	{
		GregorianCalendar gc1 = new GregorianCalendar();
		gc1.setTime(to);
		gc1.add(Calendar.MINUTE, 5);
		to = gc1.getTime();
		return to;
	}
	
	public static String getRandomValue(final Random random,
			final int lowerBound, final int upperBound, final int decimalPlaces) {

		if (lowerBound < 0 || upperBound <= lowerBound || decimalPlaces < 0) {
			throw new IllegalArgumentException("Put error message here");
		}

		final double dbl = ((random == null ? new Random() : random)
				.nextDouble() //
				* (upperBound - lowerBound))
				+ lowerBound;
		return String.format("%." + decimalPlaces + "f", dbl);

	}

	private static boolean isDay(Date d) {
		GregorianCalendar gc1 = new GregorianCalendar();
		gc1.setTime(d);
		int h = gc1.get(Calendar.HOUR_OF_DAY);
		if (h >= 8 && h < 23)
			return true;
		return false;
	}
}
